package com.designschool.base.activity;

import org.apache.http.Header;

import com.designschool.activity.R;
import com.designschool.base.http.UploadServerRunnable;
import com.designschool.base.notice.InitListener;
import com.designschool.base.util.SystemBarTintManager;
import com.designschool.base.util.ToastUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Dialog;
import android.content.Context; 
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;

/***
 * 
 * @项目名:vvvpic  
 * 
 * @类名:BaseActivity.java  
 * 
 * @创建人:shibaotong 
 *
 * @类描述:唯图网基类
 * 
 * @date:2016年4月1日
 * 
 * @Version:1.0 
 *
 *****************************************
 */
public abstract class BaseActivity extends FragmentActivity implements InitListener,OnClickListener{
    
    public AsyncHttpClient httpClient;
    
    public FragmentManager fragmentManager;
    public FragmentTransaction transaction;  
    public RequestParams params; 
    public Intent intent;
    
    private static String BASE_URL="http://www.vvvpic.com/api/";
    private  InputMethodManager inputMethodManager;
    /* 加载框 */
    private Dialog loadDialog; 
    
    public static final int AREA=1;
    public static final int SELECTAREA=2;
    public static final int REQUEST_CODE_PICK_IMAGE = 0;
    public static final int SET_PHOTO = 3;
    
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
       
        inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        loadDialog=getRequestDg(this);  
         
        httpClient=new AsyncHttpClient();      
        PersistentCookieStore myCookieStore = new PersistentCookieStore(this);    
        httpClient.setCookieStore(myCookieStore);   
        httpClient.setTimeout(10000);
        
        fragmentManager=getSupportFragmentManager();  
        
     
        /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {    
            setTranslucentStatus(true);    
        }   */ 
//        SystemBarTintManager tintManager = new SystemBarTintManager(this);    
//        tintManager.setStatusBarTintEnabled(true);    
//        tintManager.setStatusBarTintResource(R.color.red);//通知栏所需颜色  
        
        setContentView();
        
        initViews();
        
        initDatas();
        
        setDatas();
        
        setListener();
        
    }
    

    private void setTranslucentStatus(boolean on) {    
        Window win = getWindow();    
        WindowManager.LayoutParams winParams = win.getAttributes();    
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;    
        if (on) {    
            winParams.flags |= bits;    
        } else {    
            winParams.flags &= ~bits;    
        }    
        win.setAttributes(winParams);    
    }  
    
    public void post(final String requestname, RequestParams params){
       
       
        httpClient.post(this, BASE_URL+requestname, params,  new AsyncHttpResponseHandler()
        {
           
            @Override
            public void onStart() { 
                show();
            }
            
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] response) { 
//                Log.e(requestname, new String(response));
                Log.e("statusCode==>", statusCode+"");
                OnReceive(requestname, new String(response));
                disMiss();
                for (Header h : headers) {
                    Log.e(h.getName(), h.getValue());
                }
               
            }
             
            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] response, Throwable arg3) { 
                Log.e("statusCode==>", statusCode+"");
                if(statusCode==0){
                ToastUtils.show(BaseActivity.this, "请检查你的网络状况");
                }
                disMiss();
            }
        });
        
    }
    
    public void pullpost(final String requestname, RequestParams params){
        
        httpClient.post(this, BASE_URL+requestname, params,  new AsyncHttpResponseHandler()
        { 
            @Override
            public void onStart() {  
            } 
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] response) { 
//                Log.e(requestname, new String(response));
                OnReceive(requestname, new String(response)); 
                disMiss();
                
            }
            @Override
            public void onProgress(long bytesWritten, long totalSize) {
                BaseActivity.this.onProgress(bytesWritten, totalSize);
            }
        
            
            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] response, Throwable arg3) { 
               /* ToastUtils.show(BaseActivity.this, "请检查你的网络状况");
                disMiss();*/
            	disMiss();
            }
        }); 
    }
    
  
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        ResumeDatas();
    }
    
    /***
     * 显示加载对话框
     */
    public void show() {
        
        if(loadDialog!=null){
            loadDialog.show();
        }
       
    }

    public void disMiss(){
       if(loadDialog!=null){
           loadDialog.dismiss();
       }
    }

    public void Jump(Intent intent){
        startActivity(intent);
    }
   
    /***
     * 设置加载框样式
     * 
     * @param context
     * @return
     */
    private Dialog getRequestDg(Context context) {
        Dialog dgloading;
        dgloading = null;
        dgloading = new Dialog(context,R.style.loadingDialog);
        LinearLayout layout = new LinearLayout(context);

        layout.setBackgroundColor(context.getResources().getColor(
                R.color.transparent));
        View view = LayoutInflater.from(this).inflate(R.layout.bar, null);
        layout.addView(view); 
        dgloading.setContentView(layout);
        dgloading.setCanceledOnTouchOutside(false); 
        
        return dgloading;
    }
    
    
    public void OnReceive(String requestname,String response){
        
    } 
    
    public void onProgress(long bytesWritten, long totalSize ){
        
    }
    
    /***
     * 跳转无参数
     * @param cls
     */
    public void Jump(Class cls)
    {
        intent=new Intent(this, cls);
        startActivity(intent);
    }
    
    /***
     * 跳转有返回
     * @param cls
     */
    public void Jump(Intent intent,int code)
    {
        startActivityForResult(intent, code);
    }
    
    /***
     * 替换当前页面显示
     * @param fragment
     */
    public void setReplace(Fragment fragment) { 
        
    }
    
    public void hideSoftInput(EditText editText) {
        if (inputMethodManager != null) {
            View v = this.getCurrentFocus();
            if (v == null) {
                return;
            }
     
            inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(),
                                                       InputMethodManager.HIDE_NOT_ALWAYS);
            editText.clearFocus();
        }
    }
    
    
    @Override
    protected void onDestroy() { 
        ImageLoader.getInstance().clearMemoryCache();
        System.gc();
        super.onDestroy();
    }
   
}
